//
//  SISSComboBoxView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/10.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSComboBoxView.h"

#import "SISSAreaModel.h"


@interface SISSComboBoxView ()<MMComBoBoxViewDataSource, MMComBoBoxViewDelegate>

@property (nonatomic, strong) NSArray *mutableArray;

/** 判断房源|客源 */
@property (assign, nonatomic) BOOL isHouseResource;
/** 是否是进度 */
@property (assign, nonatomic) BOOL isProgress;
/** 是否是合作 */
@property (assign, nonatomic) BOOL isCooperation;
/** 价格的Title */
@property (copy, nonatomic) NSString *priceTitleStr;

/** 是否是户型筛选 */
@property (assign, nonatomic) BOOL isHouseType;

@end
@implementation SISSComboBoxView

- (instancetype)initWithFrame:(CGRect)frame isHouseResource:(BOOL)isHouseResource isCooperation:(BOOL)isCooperation {
    
    self.isHouseResource = isHouseResource;
    self.isCooperation = isCooperation;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame isHouseResource:(BOOL)isHouseResource isCooperation:(BOOL)isCooperation priceTitleStr:(NSString *)priceTitleStr {
    
    self.isHouseResource = isHouseResource;
    self.isCooperation = isCooperation;
    self.priceTitleStr = priceTitleStr;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame isHouseResource:(BOOL)isHouseResource isCooperation:(BOOL)isCooperation isProgress:(BOOL)isProgress {
    
    self.isHouseResource = isHouseResource;
    self.isCooperation = isCooperation;
    self.isProgress = isProgress;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithHouseTypeFrame:(CGRect)frame {
    self.isHouseType = YES;
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.comBoBoxView = [[MMComBoBoxView alloc] initWithFrame:self.bounds];
    self.comBoBoxView.dataSource = self;
    self.comBoBoxView.delegate = self;
    [self addSubview:self.comBoBoxView];
    [self.comBoBoxView reload];
}



#pragma mark - MMComBoBoxViewDataSource
- (NSUInteger)numberOfColumnsIncomBoBoxView :(MMComBoBoxView *)comBoBoxView {
    return self.mutableArray.count;
}
- (MMItem *)comBoBoxView:(MMComBoBoxView *)comBoBoxView infomationForColumn:(NSUInteger)column {
    return self.mutableArray[column];
}

#pragma mark - MMComBoBoxViewDelegate
- (void)comBoBoxView:(MMComBoBoxView *)comBoBoxViewd didSelectedItemsPackagingInArray:(NSArray *)array atIndex:(NSUInteger)index {
    MMItem *rootItem = self.mutableArray[index];
    switch (rootItem.displayType) {
        case MMPopupViewDisplayTypeNormal: {
            //拼接选择项
            NSMutableString *title = [NSMutableString string];
            __block NSInteger firstPath;
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                [title appendString:idx?[NSString stringWithFormat:@";%@",[rootItem findTitleBySelectedPath:path]]:[rootItem findTitleBySelectedPath:path]];
                if (idx == 0) {
                    firstPath = path.firstPath;
                }
            }];
            
            NSLog(@"当title为%@时，所选字段为 %@",rootItem.title ,title);
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:title:)]) {
                
                [self.delegate comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:rootItem.title title:title];
            }
            
            break;
        }
            
        case MMPopupViewDisplayTypeMultilayer: {
            
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                
                // 拿到地区的前一个
                MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                MMItem *thirdItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath].childrenNodes[path.thirdPath];
                NSLog(@" rootItem.title:%@  当firstItem.title为%@时，所选字段secondItem.title为 %@,--thirdItem.title %@",rootItem.title,firstItem.title,secondItem.title,thirdItem.title);
                
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:title:subtitle:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:rootItem.title title:secondItem.title subtitle:thirdItem.title];
                }
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:firstTitle:secondTitle:thirdTitle:)]) {
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:rootItem.title firstTitle:firstItem.title secondTitle:secondItem.title thirdTitle:thirdItem.title];
                }
            }];
            
            break;
        }
        case MMPopupViewDisplayTypeFilters:{
            MMCombinationItem *combinationItem = self.mutableArray[index];
            [array enumerateObjectsUsingBlock:^(NSMutableArray*  _Nonnull subArray, NSUInteger idx, BOOL * _Nonnull stop) {
                if (combinationItem.isHasSwitch && idx == 0) {
                    for (MMSelectedPath *path in subArray) {
                        MMAlternativeItem *alternativeItem = combinationItem.alternativeArray[path.firstPath];
                        NSLog(@"当title为: %@ 时，选中状态为: %d",alternativeItem.title,alternativeItem.isSelected);
                       
                    }
                    return;
                }
                
                NSString *title;
                NSMutableArray *subtitles = [NSMutableArray array];
                for (MMSelectedPath *path in subArray) {
                    MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                    MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                    title = firstItem.title;
                    [subtitles addObject:secondItem.title];
//                    [subtitles appendString:[NSString stringWithFormat:@"  %@",secondItem.title]];
                }
                NSLog(@"当title为%@时，所选字段为 %@",title,subtitles);
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeFiltersWithTitle:subtitles:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeFiltersWithTitle:title subtitles:subtitles.copy];
                }
            }];
            
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDisplayTypeFiltersConfirmClickAction)]) {
                [self.delegate comboBoxViewDisplayTypeFiltersConfirmClickAction];
            }
            
            break;}
        default:
            break;
    }
}



- (NSArray *)mutableArray {
    
    
    if (_mutableArray == nil) {
        if (self.isHouseType) {
            NSMutableArray *mutableArray = [NSMutableArray array];
            
            // 户型
            NSArray *houseTypeArr = @[@"不限", @"一房", @"二房", @"三房", @"四房", @"四房以上", @"复式", @"别墅", @"其他"];
            MMSingleItem *rootItemHouseType = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName: @"户型"];
            //        rootItemHouseType.selectedType = MMPopupViewMultilSeMultiSelection;
            for (int i = 0; i < houseTypeArr.count; i++) {
                [rootItemHouseType addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:houseTypeArr[i]]];
            }
            
            // 价格
            NSArray *priceArr = @[@"不限", @"200万以下", @"200-300万", @"300-500万", @"500-800万", @"800-1200万", @"1200万-2000万", @"2000万以上"];
            MMSingleItem *rootItemPrice = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:@"价格"];
            for (int i = 0; i < priceArr.count; i++) {
                [rootItemPrice addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:priceArr[i]]];
            }
            
            // 面积
            NSArray *areanArr = @[@"不限", @"50m²以下", @"50-70m²", @"70-90m²", @"90-120m²", @"120-150m²", @"150-200m²", @"200-300m²", @"300m²以上"];
            MMSingleItem *areaItem = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"面积"];
            for (int i = 0; i < areanArr.count; i++) {
                [areaItem addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:areanArr[i]]];
            }
            
            // 建筑形式
            NSArray *buildingTypeArr = @[@"不限", @"小层", @"高层", @"中高层", @"别墅"];
            MMSingleItem *buildingTypeItem = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"建筑形式"];
            for (int i = 0; i < buildingTypeArr.count; i++) {
                [buildingTypeItem addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:buildingTypeArr[i]]];
            }
            
            // 状态
            NSArray *sellStatusArr = @[@"不限", @"售罄", @"在售", @"已预订"];
            MMSingleItem *sellStatusItem = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"状态"];
            for (int i = 0; i < sellStatusArr.count; i++) {
                [sellStatusItem addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:sellStatusArr[i]]];
            }
            
            [mutableArray addObject:rootItemHouseType];
            [mutableArray addObject:rootItemPrice];
            [mutableArray addObject:areaItem];
            [mutableArray addObject:buildingTypeItem];
            [mutableArray addObject:sellStatusItem];
            _mutableArray  = [mutableArray copy];
        } else {
            NSMutableArray *mutableArray = [NSMutableArray array];
            NSArray<SISSAreaModel *> *areaArr = [SISSAreaModel loadAreaData];
            
            NSString *areaTitleName = self.isHouseResource ? @"区域" : @"意向区域";
            // 区域
            MMMultiItem *rootItemArea = [MMMultiItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaTitleName];
            rootItemArea.displayType = MMPopupViewDisplayTypeMultilayer;
            rootItemArea.numberOflayers = MMPopupViewThreelayers;
            NSArray *arrA1 = @[@"区域",@"地铁",@"周边"];
            for (int i = 0; i < arrA1.count; i++) {
                MMItem *rootItemArea_A = [MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:arrA1[i]];
                rootItemArea_A.isSelected = (i == 0);
                [rootItemArea addNode:rootItemArea_A];
                for (int j = 0; j < areaArr.count; j++){
                    SISSAreaModel *areaModel = areaArr[j];
                    
                    MMItem *rootItemArea_B = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaModel.name];
                    rootItemArea_B.isSelected = (j == 0);
                    [rootItemArea_A addNode:rootItemArea_B];
                    for (int k = 0; k < areaModel.areas.count; k ++) {
                        
                        MMItem *rootItemArea_C = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaModel.areas[k]];
                        rootItemArea_C.isSelected = (k == 0);
                        [rootItemArea_B addNode:rootItemArea_C];
                    }
                }
            }
            
            // 售价
            NSString *priceTitleName = (self.priceTitleStr && self.priceTitleStr.length > 0) ? self.priceTitleStr : (self.isHouseResource ? @"售价" : @"预算");
            NSArray *priceArr = @[@"不限", @"200万以下", @"200-300万", @"300-500万", @"500-800万", @"800-1200万", @"1200万-2000万", @"2000万以上"];
            priceArr = (self.priceTitleStr && self.priceTitleStr.length > 0) ? @[@"不限", @"500元以下", @"500-1000元", @"1000-2000元", @"2000-3000元", @"3000-5000元", @"5000-8000元", @"8000-11000元", @"11000元以上"] : priceArr;
            MMSingleItem *rootItemPrice = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:priceTitleName];
            for (int i = 0; i < priceArr.count; i++) {
                [rootItemPrice addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:priceArr[i]]];
            }
            
            // 户型
            NSArray *houseTypeArr = @[@"不限", @"一房", @"二房", @"三房", @"四房", @"四房以上", @"复式", @"别墅", @"其他"];
            MMSingleItem *rootItemHouseType = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:(self.priceTitleStr && self.priceTitleStr.length > 0 && !self.isHouseResource) ? @"意向户型" : @"户型"];
            //        rootItemHouseType.selectedType = MMPopupViewMultilSeMultiSelection;
            for (int i = 0; i < houseTypeArr.count; i++) {
                [rootItemHouseType addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:houseTypeArr[i]]];
            }
            
            
            // 更多
            MMCombinationItem *rootItemMore = [MMCombinationItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"更多"];
            rootItemMore.displayType = MMPopupViewDisplayTypeFilters;
            rootItemMore.selectedType = MMPopupViewMultilSeMultiSelection;
            
            
            NSDictionary *dict = self.isCooperation ? @{@"合作范围":@[@"门店内合作", @"公司内合作"]} : @{@"可见范围":@[@"部分可见"]};
            
            NSArray *arr1 = @[
                              dict,
                              @{@"面积":@[@"50m²以下", @"50-70m²", @"70-90m²", @"90-120m²", @"120-150m²", @"150-200m²", @"200-300m²", @"300m²以上"]},
                              @{@"朝向":@[@"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西南", @"西北"]},
                              @{@"装修程度":@[@"毛坯", @"普装", @"精装", @"豪装"]}
                              ];
            NSArray *arr2 = @[
                              @{@"合作身份":@[@"我是发布方", @"我是合作方"]},
                              @{@"合作范围":@[@"门店内合作", @"公司内合作"]},
                              @{@"面积":@[@"50m²以下", @"50-70m²", @"70-90m²", @"90-120m²", @"120-150m²", @"200-300m²", @"300m²以上"]},
                              @{@"朝向":@[@"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西南", @"西北"]},
                              @{@"装修程度":@[@"毛坯", @"普装", @"精装", @"豪装"]}
                              ];
            
            NSArray *arr = self.isProgress ? arr2 : arr1;
            
            for (NSDictionary *itemDic in arr) {
                MMItem *itemMore_A = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:[itemDic.allKeys lastObject]];
                [rootItemMore addNode:itemMore_A];
                for (int i = 0; i <  [[itemDic.allValues lastObject] count]; i++) {
                    NSString *title = [itemDic.allValues lastObject][i];
                    MMItem *itemMore_B = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:title];
                    //                if (i == 0) {
                    //                    item4_B.isSelected = YES;
                    //                }
                    [itemMore_A addNode:itemMore_B];
                }
            }
            
            [mutableArray addObject:rootItemArea];
            [mutableArray addObject:rootItemPrice];
            [mutableArray addObject:rootItemHouseType];
            [mutableArray addObject:rootItemMore];
            _mutableArray  = [mutableArray copy];
        }
    }
    return _mutableArray;
}

@end
